﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace DotNET4Study
{
    public static class IEnumerableStudy
    {
        public static void Execute()
        {
            Foo f = new Foo();
            
            foreach (var v in f)
            {
                Console.WriteLine(v);
            }

            Boo b = new Boo();
            foreach (var v in b)
            {
                Console.WriteLine(v);
            }

            foreach (var v in (IEnumerable<string>)b)
            {
                Console.WriteLine(v);
            }
        }

        public class Foo : IEnumerable<string>
        {
            private List<string> aList = new List<string>() { "a","b","c"};
            private List<string> bList = new List<string>() { "x","y","z"};

            public IEnumerator<string> GetEnumerator()
            {
                foreach (var v in aList)
                {
                    yield return v;
                }
            }

            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
            {
                foreach (var v in bList)
                {
                    yield return v;
                }
            }
        }


        public class Boo : IEnumerable<string>
        {
            private List<string> aList = new List<string>() { "a", "b", "c" };
            private List<string> bList = new List<string>() { "x", "y", "z" };

            public IEnumerator GetEnumerator()
            {
                foreach (var v in bList)
                {
                    yield return v;
                }
            }

            IEnumerator<string> IEnumerable<string>.GetEnumerator()
            {
                foreach (var v in aList)
                {
                    yield return v;
                }
            }
        }
    }
}
